<template>
  <div id='detail'>
    <detail-nav-bar class='detail-nav'/>
    <scroll class='content' ref='scroll'>
      <detail-swiper :top-images='topImages'/>
      <detail-base-info :goods='goods'/>
      <detail-shop-info :shop='shop'/>
      <detail-goods-info :detail-info='detailInfo' @imgLoad='imgLoad'/>
      <detail-param-info :param-info='paramInfo'/>
      <detail-comment-info :comment-info='commentInfo'/>
      <!--<goods-list :goods='recommends'/>-->
    </scroll>
  </div>
</template>

<script>
import DetailNavBar from './childComps/DetailNavBar'
import DetailSwiper from './childComps/DetailSwiper'
import DetailBaseInfo from './childComps/DetailBaseInfo'
import DetailShopInfo from './childComps/DetailShopInfo'
import DetailGoodsInfo from './childComps/DetailGoodsInfo'
import DetailParamInfo from './childComps/DetailParamInfo'
import DetailCommentInfo from './childComps/DetailCommentInfo'

import Scroll from 'components/common/scroll/Scroll'
import GoodsList from 'components/content/goods/GoodsList'

import {getDetail, Goods, Shop, GoodsParam, getRecommend} from 'network/detail'
import {debounce} from 'common/utils';

export default {
  name: 'Detail',
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    Scroll,
    GoodsList,
  },
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      itemImageListener: null
    }
  },
  created() {
    //1.保存传入的iid
    this.iid = this.$route.params.iid

    //2.请求详情数据
    getDetail(this.iid).then(res => {
      //console.log(res)
      //1.获取数据
      const data = res.result;

      //2.取出轮播图的数据
      this.topImages = data.itemInfo.topImages

      //3.获取商品信息
      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)

      //4. 创建店铺信息的对象
      this.shop = new Shop(data.shopInfo)

      //5. 保存商品的详情数据
      this.detailInfo = data.detailInfo

      //6.获取参数的信息
      this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)

      //7.取出评论的信息
      if(data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0]
      }
    })

    //3.请求推荐数据
    getRecommend().then(res => {
      console.log(res);
      this.recommends = res.data.list
    })
  },
  methods: {
    imgLoad() {
      this.$refs.scroll.refresh()
    }
  },
  mounted() {
    const refresh = debounce(this.$refs.scroll.refresh, 500)

    this.itemImageListener = () => {
      refresh()
    }

    this.$bus.$on('itemImageLoad', this.itemImageListener)
  },
  destroyed() {
   this.$bus.$off('itemImageLoad', this.itemImageListener)
  }
}
</script>

<style scoped>
  #detail {
    position: relative;
    z-index: 9;
    background-color: #fff;
    height: 100vh;
  }
  .content {
    height: calc(100% - 44px);
  }
  .detail-nav {
    position: relative;
    z-index: 9;
    background-color: #fff;
  }
</style>
